/*
 * @Descripttion:
 * @Author: ziwei
 * @Date: 2022-05-15 17:22:47
 * @LastEditors: ziwei
 * @LastEditTime: 2022-05-15 21:08:40
 */
//拿到初始state
export function createPage(ComponentClass) {
  const componentInstance = new ComponentClass();
  const initData = componentInstance.state;
  const option = {
    data: initData,
    onLoad() {
      this.$component = new ComponentClass();
      this.$component._init(this);
    },
    onReady() {
      if (typeof this.$component.componentDidMount === 'function') {
        this.$component.componentDidMount();
      }
    }
  };
  const events = ComponentClass['$$events'];
  if (events) {
    events.forEach(eventHandlerName => {
      if (option[eventHandlerName]) return;
      option[eventHandlerName] = function () {
        this.$component[eventHandlerName].call(this.$component);
      };
    });
  }
  return option;
}

export class Component {
  constructor() {
    this.state = {};
  }
  setState(state) {
    update(this.$scope.$component, state);
  }
  _init(scope) {
    console.log("scope", scope);
    this.$scope = scope;
  }
}

function update($component, state = {}) {
  $component.state = Object.assign($component.state, state);
  let data = $component.createData(state);
  data['$taroCompReady'] = true;
  $component.state = data;
  $component.$scope.setData(data);
}
